package com.jerei.plugin.control;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jerei.plugin.entity.WcmCommonParam;
import com.jerei.plugin.entity.WcmCommonParamDetail;
import com.jerei.plugin.service.IWcmCommonParamDetailService;
import com.jerei.plugin.service.IWcmCommonParamService;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.UtilsCondition;
import com.jerei.utils.spring.PageBean;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

import java.util.List;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
public class WcmCommonParamControl extends PluginController {

    @Autowired
    private IWcmCommonParamService iWcmCommonParamService;

    @Autowired
    private IWcmCommonParamDetailService iWcmCommonParamDetailService;

    @RequiresPermissions({"common-param:save", "common-param:update"})
    public String leftSave() {
        WcmCommonParam entity = this.wrapper(WcmCommonParam.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonParamService.update(entity);
        } else {
            iWcmCommonParamService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-param:save", "common-param:update"})
    public String rightSave() {
        WcmCommonParamDetail entity = this.wrapper(WcmCommonParamDetail.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonParamDetailService.update(entity);
        } else {
            iWcmCommonParamDetailService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-param:remove"})
    public String leftDelete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonParamService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"common-param:remove"})
    public String rightDelete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonParamDetailService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"common-param:save", "common-param:update", "common-param:view"})
    public void leftEdit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonParam entity = null;
        if (null != id) {
            entity = iWcmCommonParamService.find(id);
        } else {
            entity = new WcmCommonParam();
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"common-param:save", "common-param:update", "common-param:view"})
    public void rightEdit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonParamDetail entity = null;
        if (null != id) {
            entity = iWcmCommonParamDetailService.find(id);
        } else {
            Long paramId = UtilsWeb.getLong(this.getRequest(), "paramId");
            entity = new WcmCommonParamDetail();
            entity.setParamId(paramId);
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"common-param:view"})
    public String leftPage() {
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        PageBean<WcmCommonParam> pageBean = iWcmCommonParamService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(this.getRequest().getParameterMap()));
        return UtilsUI.datagrid(pageBean);
    }

    @RequiresPermissions({"common-param:view"})
    public String rightPage() {
        Long paramId = UtilsWeb.getLong(this.getRequest(), "search.eq.paramId");
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        if (null != paramId && paramId > 0L) {
            return UtilsUI.datagrid(iWcmCommonParamDetailService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(this.getRequest().getParameterMap())));
        } else {
            return UtilsUI.datagrid();
        }
    }

    @RequiresPermissions({"common-param:view"})
    public String paramDetails() {
        return JSON.toJSONString(iWcmCommonParamDetailService.getAll());
    }

    @RequiresPermissions({"common-param:view"})
    public String params() {
        String no = UtilsWeb.getString(this.getRequest(), "no");
        if ("".equals(no)) {
            return new JSONArray().toJSONString();
        }
        WcmCommonParam param = iWcmCommonParamService.findByNo(no);
        if (null == param) {
            return new JSONArray().toJSONString();
        }
        List<WcmCommonParamDetail> list = iWcmCommonParamDetailService.getByParamId(param.getId());
        if (null == list || list.size() <= 0) {
            return new JSONArray().toJSONString();
        }
        return JSON.toJSONString(list);
    }

}